package com.mvvm.start.main.mvvm.v.activity


import android.R
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ImageSpan
import android.view.View
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.mvvm.start.base.base.BaseActivity
import com.mvvm.start.base.utils.loge
import com.mvvm.start.common.constant.RouteUrl
import com.mvvm.start.main.R
import com.mvvm.start.main.databinding.ActivityTestBinding
import com.mvvm.start.main.mvvm.vm.PagingViewModle
import com.mvvm.start.main.utils.CustomLinkMovementMethod
import com.mvvm.start.main.utils.MeetRecordContentTextData
import com.mvvm.start.main.utils.StringDiscolorationHelper
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch


/**
 * @功能: Pagin3的使用方法
 * @User Lmy
 * @Creat 2021/11/1 10:32 上午
 * @Compony 永远相信美好的事情即将发生
 */
@Route(path = RouteUrl.TestActivity)
class TestActivity : BaseActivity<PagingViewModle, ActivityTestBinding>() {
    var removeView: View? = null
    override fun initView() {
        val dataList = mutableListOf(MeetRecordContentTextData().apply {
            type = "normal"
            color = "#333333"
            text = "哈哈哈哈哈"
        }, MeetRecordContentTextData().apply {
            type = "normal"
            color = "#FC4A7F"
            text = "阿萨哈哈哈"
        }, MeetRecordContentTextData().apply {
            type = "img"
            color = "#333333"
            text = "http://image2.520txy.com/affinity/rob/img/rob_cp_gift_level_5.png"
        }, MeetRecordContentTextData().apply {
            type = "normal"
            color = "#FC4A7F"
            text = "哈哈哈哈哈哈哈啊时间啊今年卢卡斯啦哈哈哈哈哈哈哈哈哈哈哈哈"
        }, MeetRecordContentTextData().apply {
            type = "normal"
            color = "#333333"
            text = "哈哈哈哈哈哈哈哈哈哈"
        })
        viewBinding.tvContent.highlightColor=Color.TRANSPARENT
        viewBinding.tvContent.movementMethod = CustomLinkMovementMethod()
        viewBinding.tvContent.text=StringDiscolorationHelper.instance.textIconStringDiscoloration(dataList,viewBinding.tvContent)
        viewBinding.llAll.setOnClickListener {
            viewBinding.tvContent.text=StringDiscolorationHelper.instance.textIconStringDiscoloration(dataList,viewBinding.tvContent)
        }
        val textView =viewBinding.tvContent
        val spannableString = SpannableString("  " + textView.text)
        val defaultSpan: ImageSpan = CenterVerticalImageSpan(this, com.mvvm.start.main.R.drawable.ic_main_me)
        spannableString.setSpan(defaultSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        textView.setText(spannableString, TextView.BufferType.SPANNABLE)

        val imageUrl: String = imageSpan.getString("image") // 图片链接


        if (!TextUtils.isEmpty(imageUrl)) {
            val remoteImageView = RemoteImageView(context)
            remoteImageView.setLoadOriginal(true)
            remoteImageView.setImageLoadListener(object : PainterImageLoadListener() {
                fun onHandleResourceReady(imageView: ImageView?, drawable: Any): Boolean {
                    val start = spannableString.getSpanStart(defaultSpan)
                    val end = spannableString.getSpanEnd(defaultSpan)
                    if (start != -1 && end != -1) {
                        if (drawable is Drawable) {
                            (drawable as Drawable).setBounds(
                                0, 0, (drawable as Drawable).getIntrinsicWidth(),
                                (drawable as Drawable).getIntrinsicHeight()
                            )
                            spannableString.removeSpan(defaultSpan)
                            spannableString.setSpan(
                                CenterVerticalImageSpan(drawable as Drawable), start, end,
                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                            )
                            textView.setText(spannableString, TextView.BufferType.SPANNABLE)
                            textView.requestLayout()
                        }
                    }
                    return false
                }

                fun onHandleLoadFailed(imageView: ImageView?): Boolean {
                    return false
                }
            })
            remoteImageView.load(imageUrl)
        }


//        var view1 = View(this)
//        view1.setBackgroundResource(R.drawable.button_bg)
//        view1.layoutParams = RelativeLayout.LayoutParams(
//            RelativeLayout.LayoutParams.MATCH_PARENT, 300.dp.toInt()
//        )
//        viewBinding.rlAll.addView(view1)
//        removeView = view1
//        viewBinding.rlAll.setOnClickListener {
//            val animationExit = AnimationUtils.loadAnimation(mContext, R.anim.gift_exit)
//            animationExit.setAnimationListener(object : Animation.AnimationListener {
//                override fun onAnimationStart(animation: Animation?) {
//
//                }
//
//                override fun onAnimationEnd(animation: Animation?) {
//                }
//
//                override fun onAnimationRepeat(animation: Animation?) {
//                }
//            })
//            removeView?.startAnimation(animationExit)
//            viewBinding.rlAll.removeView(removeView)
//            var view2 = View(this)
//            view2.layoutParams = RelativeLayout.LayoutParams(
//                RelativeLayout.LayoutParams.MATCH_PARENT, 300.dp.toInt()
//            )
//            view2.setBackgroundResource(R.drawable.button_bg2)
//            val animationEnter = AnimationUtils.loadAnimation(mContext, R.anim.gift_enter)
//            animationEnter.startOffset = 100
//            viewBinding.rlAll.addView(view2)
//            view2.startAnimation(animationEnter)
//            removeView = view2
//        }
    }

    override fun initObserve() {
        lifecycleScope.launch {
            mViewModel.sharedFlow.collect {
                "sharedFlow:$it".loge()
            }
        }
    }

    override fun onResume() {
        super.onResume()
    }

    override fun initData() {

    }


}


